parameters:
  buildConfig: ''
  osGroup: ''
  osSubgroup: ''
  archType: ''
  targetRid: ''
  isOfficialBuild: false
  liveRuntimeBuildConfig: ''
  runtimeFlavor: 'coreclr'
  runtimeDisplayName: 'coreclr'
  interpreter: ''
  timeoutInMinutes: 150
  pool: ''
  runtimeVariant: ''
  testScope: ''
  helixQueues: []
  condition: true
  shouldContinueOnError: false
  variables: {}
  scenarios: []
  SuperPmiCollect: false
  SuperPmiCollectionType: 'run'
  SuperPmiCollectionName: 'libraries_tests'
  dependsOn: []
  unifiedArtifactsName: ''
  helixArtifactsName: ''
  unifiedBuildNameSuffix: ''
  unifiedBuildConfigOverride: ''

jobs:
  - template: /eng/common/templates/job/job.yml
    parameters:
      enablePublishBuildArtifacts: true
      timeoutInMinutes: ${{ parameters.timeoutInMinutes }}
      container: ${{ parameters.container }}
      condition: and(succeeded(), ${{ parameters.condition }})
      helixRepo: dotnet/runtime
      ${{ if eq(parameters.SuperPmiCollect, true) }}:
        displayName: ${{ format('Libraries SuperPMI collection {0} {1} {2} {3}{4} {5} {6}', parameters.SuperPmiCollectionName, parameters.liveRuntimeBuildConfig, parameters.runtimeDisplayName, parameters.osGroup, parameters.osSubgroup, parameters.archType, parameters.buildConfig) }}
        name: ${{ format('spmi_{0}_{1}_{2}_{3}{4}_{5}_{6}', parameters.SuperPmiCollectionName, parameters.liveRuntimeBuildConfig, parameters.runtimeDisplayName, parameters.osGroup, parameters.osSubgroup, parameters.archType, parameters.buildConfig) }}
      ${{ else }}:
        ${{ if ne(parameters.liveRuntimeBuildConfig, '') }}:
          displayName: ${{ format('Libraries Test Run {0} {1} {2}{3} {4} {5}', parameters.liveRuntimeBuildConfig, parameters.runtimeDisplayName, parameters.osGroup, parameters.osSubgroup, parameters.archType, parameters.buildConfig) }}
          name: ${{ format('libraries_test_run_{0}_{1}_{2}{3}_{4}_{5}', parameters.liveRuntimeBuildConfig, parameters.runtimeDisplayName, parameters.osGroup, parameters.osSubgroup, parameters.archType, parameters.buildConfig) }}
        ${{ if eq(parameters.liveRuntimeBuildConfig, '') }}:
          displayName: 'Test Run'
          name: test_run

      # To run the tests we just send to helix and wait, use ubuntu hosted pools for faster providing and to not back up our build pools
      pool: ${{ parameters.pool }}

      # Component governance does not work on musl machines
      ${{ if eq(parameters.osSubGroup, '_musl') }}:
        disableComponentGovernance: true

      dependsOn:
      - ${{ if ne(parameters.dependsOn[0], '') }}:
        - ${{ parameters.dependsOn }}
      - ${{ else }}:
        - 'build_${{ parameters.osGroup }}${{ parameters.osSubgroup }}_${{ parameters.archType }}_${{ coalesce(parameters.unifiedBuildConfigOverride, parameters.buildConfig) }}_${{ parameters.unifiedBuildNameSuffix }}'

      workspace:
        clean: all

      variables:
        - ${{ if eq(variables['System.TeamProject'], 'internal') }}:
          - group: DotNet-HelixApi-Access
          - group: AzureDevOps-Artifact-Feeds-Pats

        - _archiveTestsParameter: /p:ArchiveTests=true

        - ${{ if eq(parameters.SuperPmiCollect, true) }}:
          - template: /eng/pipelines/coreclr/templates/jit-python-variables.yml
            parameters:
              osGroup: ${{ parameters.osGroup }}
          - template: /eng/pipelines/libraries/superpmi-collect-variables.yml
            parameters:
              buildConfig: ${{ parameters.buildConfig }}
              osGroup: ${{ parameters.osGroup }}
              archType: ${{ parameters.archType }}
              runtimeFlavor: ${{ parameters.runtimeFlavor }}

        - ${{ parameters.variables }}

      steps:
      - checkout: self
        clean: true
        fetchDepth: $(checkoutFetchDepth)

      - ${{ if ne(variables['System.TeamProject'], 'public') }}:
        - ${{ if ne(parameters.osGroup, 'windows') }}:
          - task: Bash@3
            displayName: Setup Private Feeds Credentials
            inputs:
              filePath: $(Build.SourcesDirectory)/eng/common/SetupNugetSources.sh
              arguments: $(Build.SourcesDirectory)/NuGet.config $Token
            env:
              Token: $(dn-bot-dnceng-artifact-feeds-rw)
        - ${{ if eq(parameters.osGroup, 'windows') }}:
          - task: PowerShell@2
            displayName: Setup Private Feeds Credentials
            inputs:
              filePath: $(Build.SourcesDirectory)/eng/common/SetupNugetSources.ps1
              arguments: -ConfigFile $(Build.SourcesDirectory)/NuGet.config -Password $Env:Token
            env:
              Token: $(dn-bot-dnceng-artifact-feeds-rw)

      - ${{ if in(parameters.osGroup, 'osx', 'maccatalyst', 'ios', 'iossimulator', 'tvos', 'tvossimulator') }}:
        - script: $(Build.SourcesDirectory)/eng/install-native-dependencies.sh ${{ parameters.osGroup }}
          displayName: Install Build Dependencies

      - template: /eng/pipelines/common/download-artifact-step.yml
        parameters:
          unpackFolder: $(Build.SourcesDirectory)/artifacts/bin
          artifactFileName: '${{ parameters.unifiedArtifactsName }}$(archiveExtension)'
          artifactName: '${{ parameters.unifiedArtifactsName }}'
          displayName: 'unified artifacts'

      - template: /eng/pipelines/common/download-artifact-step.yml
        parameters:
          displayName: Test Assets
          artifactName: ${{ parameters.helixArtifactsName }}
          artifactFileName: '${{ parameters.helixArtifactsName }}$(archiveExtension)'
          unpackFolder: $(Build.SourcesDirectory)/artifacts/helix

      - template: /eng/pipelines/libraries/helix.yml
        parameters:
          runtimeFlavor: ${{ parameters.runtimeFlavor }}
          osGroup: ${{ parameters.osGroup }}
          targetRid: ${{ parameters.targetRid }}
          archType: ${{ parameters.archType }}
          buildConfig: ${{ parameters.buildConfig }}
          helixQueues: ${{ parameters.helixQueues }}
          testScope: ${{ parameters.testScope }}
          interpreter: ${{ parameters.interpreter }}
          shouldContinueOnError: ${{ parameters.shouldContinueOnError }}
          creator: dotnet-bot
          ${{ if eq(parameters.interpreter, 'true') }}:
            testRunNamePrefixSuffix: ${{ parameters.runtimeFlavor }}_interpreter_${{ parameters.liveRuntimeBuildConfig }}
          ${{ else }}:
            testRunNamePrefixSuffix: ${{ parameters.runtimeDisplayName }}_${{ parameters.liveRuntimeBuildConfig }}
          SuperPmiCollect: ${{ parameters.SuperPmiCollect }}
          SuperPmiCollectionType: ${{ parameters.SuperPmiCollectionType }}
          SuperPmiCollectionName: ${{ parameters.SuperPmiCollectionName }}
          ${{ if eq(parameters.SuperPmiCollect, true) }}:
            extraHelixArguments: /p:RuntimeConfiguration=${{ parameters.liveRuntimeBuildConfig }}

          ${{ if ne(parameters.scenarios[0], '') }}:
            scenarios: ${{ parameters.scenarios }}

      - ${{ if eq(parameters.SuperPmiCollect, true) }}:
        - template: /eng/pipelines/libraries/superpmi-postprocess-step.yml
          parameters:
            buildConfig: ${{ parameters.buildConfig }}
            buildConfigUpper: $(buildConfigUpper)
            osGroup: ${{ parameters.osGroup }}
            osSubgroup: ${{ parameters.osSubgroup }}
            archType: ${{ parameters.archType }}
            SuperPmiCollectionType: ${{ parameters.SuperPmiCollectionType }}
            SuperPmiCollectionName: ${{ parameters.SuperPmiCollectionName }}
            MergedMchFileLocation: $(MergedMchFileLocation)
            MchFilesLocation: $(MchFilesLocation)
            SpmiLogsLocation: $(SpmiLogsLocation)
            SuperPmiMcsPath: $(SuperPmiMcsPath)
            PythonSetupScript: $(PythonSetupScript)
            PythonScript: $(PythonScript)
            PipScript: $(PipScript)
